<?php

namespace backend\modules\orders\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\orders\models\Invoice;

/**
 * InvoiceSearch represents the model behind the search form about `backend\modules\orders\models\Invoice`.
 */
class InvoiceSearch extends Invoice
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['invoice_id', 'order_id', 'p_tel', 'c_name', 'c_code', 'p_email', 'order_sn'], 'safe'],
            [['invoice_type', 'is_payed', 'porc'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Invoice::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'invoice_type' => $this->invoice_type,
            'is_payed' => $this->is_payed,
            'porc' => $this->porc,
        ]);

        $query->andFilterWhere(['like', 'invoice_id', $this->invoice_id])
            ->andFilterWhere(['like', 'order_id', $this->order_id])
            ->andFilterWhere(['like', 'p_tel', $this->p_tel])
            ->andFilterWhere(['like', 'c_name', $this->c_name])
            ->andFilterWhere(['like', 'c_code', $this->c_code])
            ->andFilterWhere(['like', 'p_email', $this->p_email])
            ->andFilterWhere(['like', 'order_sn', $this->order_sn]);

        return $dataProvider;
    }
}
